﻿using DGSFramework;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Script.Serialization;

namespace Framework.Resources
{
    /// <summary>
    /// Summary description for WatchList
    /// </summary>
    public class WatchList : IHttpHandler
    {
        DGSFrameworkEntities entity = new DGSFrameworkEntities();
        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "application/json";
            JavaScriptSerializer jsonSerializer = new JavaScriptSerializer();
            var status = 0;
            try
            {
                //kiem tra quyen 
                var stockCode = HttpContext.Current.Request.Form["stock"].ToString();
                var action = HttpContext.Current.Request.Form["action"].ToString();
                var stock = entity.dgs_Stocks.Where(d => d.Code == stockCode).FirstOrDefault();
                if (stock != null)
                {
                    if (action == "1")//add watchlist buy
                    {
                        if (!stock.IsWatchListBuy.Value) //chua co trong watchlist buy
                        {
                            stock.IsWatchListBuy = true;
                            entity.SaveChanges();
                            status = 1;
                        }
                        else //da co trong watchlist buy
                        {
                            status = -1;
                        }
                    }
                    else if (action == "2") //remove watchlist buy
                    {
                        if (stock.IsWatchListBuy.Value)
                        {
                            stock.IsWatchListBuy = true;
                            entity.SaveChanges();
                            status = 2;
                        }
                        else //stock khong co trong watchlist buy
                        {
                            status = -2;
                        }
                    }
                    else if (action == "3") //add watchlist sell
                    {
                        if (!stock.IsWatchListSell.Value) //chua co trong watchlist sell
                        {
                            stock.IsWatchListSell = true;
                            entity.SaveChanges();
                            status = 3;
                        }
                        else //da co trong watchlist sell
                        {
                            status = -3;
                        }
                    }
                    else if (action == "4") //xoa khoi watchlist sel
                    {
                        if (stock.IsWatchListSell.Value)
                        {
                            stock.IsWatchListSell = true;
                            entity.SaveChanges();
                            status = 4;
                        }
                        else //stock khong co trong watchlist buy
                        {
                            status = -4;
                        }
                    }

                }
                context.Response.Write(
                jsonSerializer.Serialize(
                    new
                    {
                        Status = status
                    }
                )
            );
            }
            catch (Exception ex)
            {
                status = -1;
                context.Response.Write(
                jsonSerializer.Serialize(
                    new
                    {
                        Status = status
                    }
                ));
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}